package fuzzylizard.teamdocs.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Understands a File that contains other Files
 * 
 */
public class Directory extends File implements Serializable {

	/**
     * List of child files
     */
    private List<File> files = new ArrayList<File>();

    /**
     * Constructor for creating directories given a directory name
     * @param name directory name
     */
    private Directory(String name) {
        super(name);
    }

    /**
     * Static method for creating new Directories given a directory name, also sets the created date
     * and time
     *
     * @param name The directory name
     * @return A brand new directory
     */
    public static File make(String name) {
        return new Directory(name);
    }
    
    @Override
    public boolean addFile(File file) {
        if (contains(file)) {
            return false;
        }
        file.setParent(this);
        return files.add(file);
    }

    @Override
    public File removeFile(File file) {
        int indexOf = files.indexOf(file);
        if (indexOf < 0)
            return null;
        
        File file2 = files.remove(indexOf);
        file2.clearParent();
        
        return file2;
    }

    @Override
    public boolean contains(File file) {
        return files.contains(file);
    }

    @Override
    public List<File> files() {
        return files;
    }

    @Override
    public File getFile(File file) {
        int indexOf = files.indexOf(file);
        if (indexOf >= 0) {
            return files.get(indexOf);
        }
        
        return null;
    }

}